import {
	createRouter,
	createWebHistory
} from 'vue-router'
import Index from '../views/index.vue'
import Login from '../views/login.vue'
import Article from '../views/article/article.vue'
import ArticleList from '../views/article/list.vue'
import ArticleAdd from '../views/article/add.vue'

import ProductCategoryList from '../views/product_category/list.vue'
import ProductCategoryAdd from '../views/product_category/add.vue'
import ProductCategoryEdit from '../views/product_category/edit.vue'

import ProductList from '../views/product/list.vue'
import ProductAdd from '../views/product/add.vue'
import ProductEdit from '../views/product/edit.vue'


import UserList from '../views/user/list.vue'
import UserAdd from '../views/user/add.vue'


import RoleList from '../views/role/list.vue'
import RoleAdd from '../views/role/add.vue'
import RoleEdit from '../views/role/edit.vue'

import MenuList from '../views/menu/list.vue'
import MenuAdd from '../views/menu/add.vue'
import MenuRole from '../views/menu/menuRole.vue'






const routes = [{
		path: '/login',
		name: 'Login',
		component: Login
	},
	{
		path: '/',
		name: 'Index',
		component: Index,
		children: [{ //文章管理
				path: 'article',
				component: Article,
				children: [{
						path: 'list',
						component: ArticleList
					},
					{
						path: 'add',
						component: ArticleAdd
					}
				]
			},
			{ //商品分类管理
				path: 'product/category',
				component: Article,
				children: [{
						path: 'list',
						component: ProductCategoryList
					},
					{
						path: 'add',
						component: ProductCategoryAdd
					},
					{
						path: 'edit',
						component: ProductCategoryEdit
					}
				]
			},
			{ //商品管理
				path: 'product',
				component: Article,
				children: [{
						path: 'list',
						component: ProductList
					},
					{
						path: 'add',
						component: ProductAdd
					},
					{
						path: 'edit',
						component: ProductEdit
					}
				]
			},
			{ //权限管理
				path: 'auth',
				component: Article,
				children: [

					{
						path: 'user/list',
						component: UserList
					},
					{
						path: 'user/add',
						component: UserAdd
					},

					{
						path: 'role/list',
						component: RoleList
					},
					{
						path: 'role/add',
						component: RoleAdd
					},
					{
						path: 'role/edit',
						component: RoleEdit
					},

					{
						path: 'menu/list',
						component: MenuList
					},
					{
						path: 'menu/add',
						component: MenuAdd
					},
					{
						path: 'menu/role',
						component: MenuRole
					},
				]
			}
		]
	},

]

const router = createRouter({
	history: createWebHistory(process.env.BASE_URL),
	routes
})
router.beforeEach((to, from, next) => {

	let token = sessionStorage.getItem('accessToken')
	if (token) {
		next()
	} else {
		if (to.path === "/login" || to.path === '/register') {
			next()
		} else {
			next('/login')
		}
	}

})

export default router